<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Model_User extends MY_Model {
	
	public function __construct() {

		parent::__construct();

		$this->table_name = 'sys_users';

		$this->primary_key = 'sys_users.user_id';

	}
	/*
	function check_email($email,$id=0){
		$this->load->database();
			$filter = $id != 0?"AND user_id != '$id'":"";
			
			$sql = "SELECT * FROM sys_users WHERE email='$email' $filter";
			$query = $this->db->query($sql);
		
		$this->db->close();
		
		$check = true;
		if($query->num_rows() > 0)
			$check = false;
			
		return $check;
	}*/
	
	function check_username($username,$id=0){
		$this->load->database();
			$filter = $id != 0?"AND user_id != '$id'":"";
			
			$sql = "SELECT * FROM sys_users WHERE username='$username' $filter";
			$query = $this->db->query($sql);
		
		$this->db->close();
		
		$check = true;
		if($query->num_rows() > 0)
			$check = false;
			
		return $check;
	}
	
	function get_user_by($id){
		$this->load->database();	
			$sql = "SELECT * FROM sys_users WHERE user_id='$id'";
			$query = $this->db->query($sql);
		
		$this->db->close();
		
		return $query;
	}
	
	function get_all($page=0,$limit=20, $q=''){
		$this->load->database();
		$filter='';
		$page = $page*$limit;
		if($q != ''){
			$filter = " WHERE (";
			$fields = $this->db->list_fields('sys_users');
			foreach ($fields as $field){
			   $filter .= " $field LIKE '%$q%' OR" ;
			}
			
			$filter = rtrim($filter,'OR').")";
		}
		$sql = "SELECT * FROM sys_users $filter ORDER BY user_id ASC LIMIT $page,$limit;";
		$query['data'] = $this->db->query($sql);
		
		$sql = "SELECT * FROM sys_users $filter";
		$query['count'] = $this->db->query($sql);
		
		$this->db->close();
		
		return $query;
	}
	
	function add($user){
		$this->load->database();
			
		$id = 0;
		
		$this->db->trans_start();
			$this->db->insert('sys_users',$user);
			
			$id = $this->db->insert_id();
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
		{
			$this->session->set_flashdata('error','Data gagal disimpan.');
		}else{
			$this->session->set_flashdata('success','Data berhasil disimpan.');
		}
		
		$this->db->close();
		
		return $id;
	}

	function edit($id,$data){
		$this->load->database();
		$this->db->trans_start();
			$this->db->where('user_id',$id);
			$this->db->update('sys_users',$data);
		$this->db->trans_complete();
		if ($this->db->trans_status() === FALSE)
		{
			$this->session->set_flashdata('error','Data gagal disimpan.');
		}else{
			$this->session->set_flashdata('success','Data berhasil disimpan.');
		}
		$this->db->close();
	}
	/*
	function edit_group($id_agent,$id_group,$group){
		$this->load->database();
			$this->db->where('id_agent',$id_agent);
			$this->db->where('id_group',$id_group);
			$this->db->update('core_users',array("id_group"=>$group));
		$this->db->close();
	}*/
	
	function delete($id){
		$this->load->database();
		
		$this->db->trans_start();
			$this->db->query("DELETE FROM sys_users WHERE user_id='$id'");
		$this->db->trans_complete();
		
		$return = $this->db->trans_status();
		
		$this->db->close();
		return $return;
	}
	/*
	function update_by_employee($id,$data){
		$this->load->database();
		
		$this->db->trans_start();
			$this->db->where('id_employer',$id);
			$this->db->update('core_users',$data);
		$this->db->trans_complete();
		
		$return = $this->db->trans_status();
		
		$this->db->close();
		return $return;
	}
	
	function update_by_agent($id,$data){
		$this->load->database();
		
		$this->db->trans_start();
			$this->db->where('id_agent',$id);
			$this->db->update('core_users',$data);
		$this->db->trans_complete();
		
		$return = $this->db->trans_status();
		
		$this->db->close();
		return $return;
	}
	
	function delete_employee($id){
		$this->load->database();
		
		$this->db->trans_start();
			$this->db->query("DELETE FROM core_users WHERE id_employer='$id'");
		$this->db->trans_complete();
		
		$return = $this->db->trans_status();
		
		$this->db->close();
		return $return;
	}
	
	function delete_agent($id){
		$this->load->database();
		
		$this->db->trans_start();
			$this->db->query("DELETE FROM core_users WHERE id_agent='$id'");
		$this->db->trans_complete();
		
		$return = $this->db->trans_status();
		
		$this->db->close();
		return $return;
	}*/
	
	function check_password($pass){
		$id_user = $this->session->userdata('user_id');
		$pass = sha1($pass);
		$check = false;
		$this->load->database();
			$sql = "SELECT * FROM sys_users WHERE username='$id_user' AND password='$pass'";
			$query = $this->db->query($sql);
			
			if($query->num_rows() > 0)
				$check = true;
		$this->db->close();
		
		return $check;
	}
}

?>